#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAME "name="
#define FILE_NAME "filename="
#define FILE_PATH "/home/ap/sxfhts/print/%s"
#define CONTENT_TYPE "Content-Type"

static char read_str[1024*1024];
static char bound_start[100];
static char bound_end[100];
static char xml_data[1024*1024];
static char file_name[50];
static char file_data[1024*1024*10];
static FILE *outfile;

int next_line(FILE *fp);
int init_bound(FILE *fp);
int get_data(FILE *fp);
int get_param_name(char *str,char *out);

int main(void)
{
	FILE *fp = fopen("temp.txt","r+");
	if(!fp)
	{
		puts("open file error !!!");
		exit(-1);
	}
	int init_re = init_bound(fp);
	if(init_re < 0)
	{
		puts("init boundary is failed!!!");
		fclose(fp);
		exit(-1);
	}
	memset(xml_data,0x00,sizeof(xml_data));
	sprintf(xml_data,"%s","<?xml version=\"1.0\" encoding=\"UTF-8\"?><formdata_to_xml>");
	int get_re = get_data(fp);	
	if(get_re == -1)	
	{
		printf("get_data have error!!!\n");
		fclose(fp);
		exit(-1);
	}
	strcat(xml_data,"</formdata_to_xml>");
	puts(xml_data);
	fclose(fp);
	return 0;
}

int get_data(FILE *fp)
{
	char str2[50];
	int flag = 0;
	int file_data_count = 0;
	while(strcmp(read_str,bound_end))
	{
		int is_out = next_line(fp);	
		if(is_out == -1)
		{
			printf("the file with wrong end![%s]-{%d}",__func__,__LINE__);
			return -1;	
		}
		if(!strcmp(read_str,bound_start) || !strcmp(read_str,bound_end))
		{
			flag = 0;
			size_t len = strlen(xml_data);
			if(xml_data[len-1] == 0X0A)
			{
				xml_data[len-2] = '\0';
			}
			snprintf(xml_data+strlen(xml_data),sizeof(xml_data)-strlen(xml_data),"</%s>",str2);
			if(outfile != NULL)
			{
				printf("%d\n",file_data_count);
				fwrite(file_data,sizeof(char),file_data_count-2,outfile);
				fclose(outfile);
				outfile = NULL;
			}
			continue;
		}
		char *name = strstr(read_str,NAME);
		char *filename = strstr(read_str,FILE_NAME);
		char *content_type = strstr(read_str,CONTENT_TYPE);
		if(name != NULL)
		{
			memset(str2,0x00,sizeof(str2));
			int re1 = get_param_name(name,str2);
			if(re1 == -1)
			{
				printf("get_param_name get name error [%s]-[%d]\n",__func__,__LINE__);
				return -1;
			}
			snprintf(xml_data+strlen(xml_data),sizeof(xml_data)-strlen(xml_data),"<%s>",str2);
			if(filename != NULL)
			{
				char str3[50];
				memset(file_name,0x00,sizeof(file_name));
				memset(str3,0x00,sizeof(str3));
				int re2 = get_param_name(filename,str3);
				if(re2 == -1)
				{
					printf("get_param_name get filename error [%s]-[%d]\n",__func__,__LINE__);
					return -1;
				}
				snprintf(xml_data+strlen(xml_data),sizeof(xml_data)-strlen(xml_data),FILE_PATH,str3);
				snprintf(file_name,sizeof(file_name),"%s",str3);
				printf("get file_name is [%s]\n",file_name);	
			}
		}
		else if(content_type != NULL)
		{
			flag = 1;
			outfile = fopen(file_name,"w+");
			memset(file_data,0x00,sizeof(file_data));
			file_data_count = 0;
			if(outfile == NULL)
			{
				printf("create outfile failed [%s] -- [%d] \n",__func__,__LINE__);
				return -1;
			}
			do{
				is_out = next_line(fp);
				if(is_out == -1)
				{
					printf("can't read next line,the file data format was wrong! [%s]-[%d]\n",__func__,__LINE__);
					fclose(outfile);
					return -1;
				}
			}while(strlen(read_str)-2 != 0);
			continue;
		}
		else if(strlen(read_str)-2==0 && flag == 0 && outfile == NULL)
		{
			continue;
		}
		else
		{
			if(flag)
			{
				int count = 0;
				while(read_str[count] != 0X0A)
				{
					file_data[file_data_count] = read_str[count];
					file_data_count++;
					count++;
				}
				file_data[file_data_count] = 0x0A;
				file_data_count++;
				continue;
			}
			snprintf(xml_data+strlen(xml_data),sizeof(xml_data)-strlen(xml_data),"%s",read_str);
		}
	}
	return 0;
}

int init_bound(FILE *fp)
{
	while(next_line(fp) != -1)
	{
		if(strlen(read_str)-2 == 0)
		{
			continue;
		}
		else
		{
			if(strstr(read_str,"--") == NULL)
			{
				puts("the data format is wrong!!! must begin with boundary data");
				return -1;
			}
			else
			{
				snprintf(bound_start,sizeof(bound_start),"%s",read_str);		
				printf("bound_start: %s",bound_start);
				snprintf(bound_end,sizeof(bound_end),"%.*s--\r\n",(int)strlen(read_str)-2,read_str);
				printf("bound_end:   %s",bound_end);
				return 0;
			}
		}
	}
	return -1;
}



int next_line(FILE *fp)
{
	int ch , i = 0;
	memset(read_str,0x00,sizeof(read_str));
	while((ch = fgetc(fp)) != EOF)
	{
		if(ch == 0x0A)
		{
			read_str[i] = ch;
			return 0;			
		}
		read_str[i] = ch;
		i++;
	}
	return -1;
}


int get_param_name(char *str,char *out)
{
	int i = 0;
	while(*str++ != '"');
	while(str[i] != '"')
	{
		if(str[i] == 0x0A)
		{
			return -1;
		}	
		out[i] = str[i];
		i++;	
	}
	return 0;
}
